﻿using System;
using System.Collections.Generic;
using System.Web;


namespace CCPSIP.AddressBook
{
    /// <summary>CCPSIP_066：取得人員詳細資訊 API(給搜尋、我的最愛用)</summary>
    /// idHeader = 裝置 UUID 前八碼。iOS 請使用 [CCPCrypto getIdHeader] 呼叫。
    ///checkcode = 裝置限時驗證碼。iOS 請使用 [CCPCrypto getCheckcode] 呼叫。
    ///PPS_Code = 員工編號。

    public class get_person_detail_info : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            HttpRequest req = context.Request;
            HttpResponse resp = context.Response;
            resp.ContentType = "text/plain";
            resp.ContentEncoding = System.Text.Encoding.UTF8;
            DateTime reqTime = DateTime.Now;
            string resultStr = "";

            try
            {
                //【01】取得POST參數，驗證身分
                string idHeader = req.Form["idHeader"];
                string checkcode = req.Form["checkcode"];
                string PPS_Code = req.Form["PPS_Code"];

                SipUserInfo UserInfo = SipKit.GetUserInfo(idHeader, checkcode, req);
                if (UserInfo == null)
                    throw new Exception("Error_01: Cannot identify this device.");  //無法識別此裝置

                //檢查參數
                if (string.IsNullOrEmpty(PPS_Code))
                    throw new Exception("Error_02: Parameter error.");  //參數錯誤

                //【02】取得使用者Addressbook權限
                //【02-1】判斷使用者SIP註冊型態, CCP, CCP_MG, CCP_HQ, CCP_ABROAD才看的到通訊錄
                OracleAddressBookKit.Right UserRight = new OracleAddressBookKit.Right();
                if (UserInfo.UserType.StartsWith("CCP"))
                {
                    //【02-2】檢查註冊資訊
                    if (String.IsNullOrEmpty(UserInfo.PPS_CODE) || String.IsNullOrEmpty(UserInfo.DEPT_NO))
                        throw new Exception("Eooro_03: SIP registration info error.");

                    //【02-3】判斷使用者通訊錄權限(EMP_DATA, A1)
                    UserRight = OracleAddressBookKit.GetUserRight(UserInfo);
                }
                else
                {
                    throw new Exception("Error_04: Not a CCP User.");
                }

                //【03】搜尋人員詳細資料
                List<OracleAddressBookKit.PeopleDetailInfo> ReturnObject = OracleAddressBookKit.GetPersonDetailInfo(PPS_Code, UserInfo, UserRight);

                //【99】回傳結果
                resp.ContentType = "application/json";
                resp.Charset = null;
                fastJSON.JSON.Instance.Parameters.EnableAnonymousTypes = true;
                resultStr = fastJSON.JSON.Instance.ToJSON(ReturnObject);
            }
            catch (Exception ex)
            {
                resp.ContentType = "application/xml";
                resultStr = ChrisKit.Ex2Xml(ex, false);
            }
            finally
            {
                int contentLength = resp.ContentEncoding.GetByteCount(resultStr);
                resp.AppendHeader("Content-Length", contentLength.ToString());
                resp.Write(resultStr);
                resp.Flush();
                resp.Close();
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}